<?php
namespace Home\Logic;
/**
 * 短信
 * @author Administrator
 *
 */
class SandCodeLogic 
{  
	protected  $user;
	protected  $password;
	protected  $sandName;
	private    $HUAXIN_URL = "http://dx.ipyy.net/smsJson.aspx";
	
	public function __construct()
	{
		$this->user 	= 'jiangpeng';
		$this->password = '123456';
		$this->sandName = '沈阳国际纺织服装城';
	}
	
	/**
	 * 发送短信(华信)
	 */
	public function sandHuaXinCode($phone,$code_name,$get_content){
		$smsapi = $this->HUAXIN_URL;
		$pass = md5($pass);
		$time=cookie('time'.$code_name.$phone);
		if (time()-$time<180&&!empty($time)){
			return '请勿频繁操作';
		}
		$code=rand(1000, 9999);
// 		cookie('code'.$code_name.$phone,$code);
// 		cookie('time'.$code_name.$phone,time(),1800);
		$content="【".$this->sandName."】您的验证码为".$code.','.date('Y-m-d').'申请用于'.$this->sandName.$get_content.'，30分钟内有效，请勿告诉他人';
		$sendurl = $smsapi.'?action=send&userid=&account='.$this->user.'&password='.$this->password.'&mobile='.$phone.'&content='.$content.'&sendTime=&extno=';
		$result =file_get_contents($sendurl) ;
		$result = json_decode($result);
		if($result->successCounts){
		    M('PhoneCode')->add(['phone'=>$phone,'type'=>$code_name,'code'=>$code]);
			return true;
		}
		return '发送失败,请稍后重试';
	}
	/**
	 * 华信验证短信
	 */
	public function checkHuaXinPhoneCode($phone,$code,$code_name){
	    $r = M('PhoneCode')->where(['phone'=>$phone,'type'=>$code_name,'code'=>$code])->find();
		//cookie('code'.$code_name.$phone) == $code;
		if($r){
// 			cookie('code'.$code_name.$phone,null);
		    M('PhoneCode')->delete($r['id']);
			return true;
		}
		return false;
	}
}

